home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Mac / macmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-29  |  8.4 KB  |  476 lines  |  [TEXT/KAHL]

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Mac module implementation */
  26.  
  27. #include "allobjects.h"
  28. #include "modsupport.h"
  29. #include "ceval.h"
  30.  
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <errno.h>
  34.  
  35. #ifdef THINK_C
  36. #include "unix.h"
  37. #undef S_IFMT
  38. #undef S_IFDIR
  39. #undef S_IFCHR
  40. #undef S_IFBLK
  41. #undef S_IFREG
  42. #undef S_ISDIR
  43. #undef S_ISREG
  44. #endif
  45.  
  46. #include "macstat.h"
  47.  
  48. #include <fcntl.h>
  49.  
  50. #include "macdefs.h"
  51. #include "dirent.h"
  52.  
  53. #ifndef MAXPATHLEN
  54. #define MAXPATHLEN 1024
  55. #endif
  56.  
  57. /* Prototypes for Unix simulation on Mac */
  58.  
  59. int chdir PROTO((const char *path));
  60. char *getbootvol PROTO((void));
  61. char *getwd PROTO((char *));
  62. int mkdir PROTO((const char *path, int mode));
  63. DIR * opendir PROTO((char *));
  64. void closedir PROTO((DIR *));
  65. struct dirent * readdir PROTO((DIR *));
  66. int rmdir PROTO((const char *path));
  67. int sync PROTO((void));
  68. #ifdef THINK_C
  69. int unlink PROTO((char *));
  70. #else
  71. int unlink PROTO((const char *));
  72. #endif
  73.  
  74.  
  75.  
  76. static object *MacError; /* Exception mac.error */
  77.  
  78. /* Set a MAC-specific error from errno, and return NULL */
  79.  
  80. static object * 
  81. mac_error() 
  82. {
  83.     return err_errno(MacError);
  84. }
  85.  
  86. /* MAC generic methods */
  87.  
  88. static object *
  89. mac_1str(args, func)
  90.     object *args;
  91.     int (*func) FPROTO((const char *));
  92. {
  93.     char *path1;
  94.     int res;
  95.     if (!getargs(args, "s", &path1))
  96.         return NULL;
  97.     BGN_SAVE
  98.     res = (*func)(path1);
  99.     END_SAVE
  100.     if (res < 0)
  101.         return mac_error();
  102.     INCREF(None);
  103.     return None;
  104. }
  105.  
  106. static object *
  107. mac_2str(args, func)
  108.     object *args;
  109.     int (*func) FPROTO((const char *, const char *));
  110. {
  111.     char *path1, *path2;
  112.     int res;
  113.     if (!getargs(args, "(ss)", &path1, &path2))
  114.         return NULL;
  115.     BGN_SAVE
  116.     res = (*func)(path1, path2);
  117.     END_SAVE
  118.     if (res < 0)
  119.         return mac_error();
  120.     INCREF(None);
  121.     return None;
  122. }
  123.  
  124. static object *
  125. mac_strint(args, func)
  126.     object *args;
  127.     int (*func) FPROTO((const char *, int));
  128. {
  129.     char *path;
  130.     int i;
  131.     int res;
  132.     if (!getargs(args, "(si)", &path, &i))
  133.         return NULL;
  134.     BGN_SAVE
  135.     res = (*func)(path, i);
  136.     END_SAVE
  137.     if (res < 0)
  138.         return mac_error();
  139.     INCREF(None);
  140.     return None;
  141. }
  142.  
  143. static object *
  144. mac_chdir(self, args)
  145.     object *self;
  146.     object *args;
  147. {
  148.     return mac_1str(args, chdir);
  149. }
  150.  
  151. static object *
  152. mac_close(self, args)
  153.     object *self;
  154.     object *args;
  155. {
  156.     int fd, res;
  157.     if (!getargs(args, "i", &fd))
  158.         return NULL;
  159.     BGN_SAVE
  160.     res = close(fd);
  161.     END_SAVE
  162.     if (res < 0)
  163.         return mac_error();
  164.     INCREF(None);
  165.     return None;
  166. }
  167.  
  168. #ifdef MPW
  169.  
  170. static object *
  171. mac_dup(self, args)
  172.     object *self;
  173.     object *args;
  174. {
  175.     int fd;
  176.     if (!getargs(args, "i", &fd))
  177.         return NULL;
  178.     BGN_SAVE
  179.     fd = dup(fd);
  180.     END_SAVE
  181.     if (fd < 0)
  182.         return mac_error();
  183.     return newintobject((long)fd);
  184. }
  185.  
  186. #endif /* MPW */
  187.  
  188. static object *
  189. mac_fdopen(self, args)
  190.     object *self;
  191.     object *args;
  192. {
  193.     extern int fclose PROTO((FILE *));
  194.     int fd;
  195.     char *mode;
  196.     FILE *fp;
  197.     if (!getargs(args, "(is)", &fd, &mode))
  198.         return NULL;
  199.     BGN_SAVE
  200.     fp = fdopen(fd, mode);
  201.     END_SAVE
  202.     if (fp == NULL)
  203.         return mac_error();
  204.     return newopenfileobject(fp, "(fdopen)", mode, fclose);
  205. }
  206.  
  207. static object *
  208. mac_getbootvol(self, args)
  209.     object *self;
  210.     object *args;
  211. {
  212.     char *res;
  213.     if (!getnoarg(args))
  214.         return NULL;
  215.     BGN_SAVE
  216.     res = getbootvol();
  217.     END_SAVE
  218.     if (res == NULL)
  219.         return mac_error();
  220.     return newstringobject(res);
  221. }
  222.  
  223. static object *
  224. mac_getcwd(self, args)
  225.     object *self;
  226.     object *args;
  227. {
  228.     char path[MAXPATHLEN];
  229.     char *res;
  230.     if (!getnoarg(args))
  231.         return NULL;
  232.     BGN_SAVE
  233.     res = getwd(path);
  234.     END_SAVE
  235.     if (res == NULL) {
  236.         err_setstr(MacError, path);
  237.         return NULL;
  238.     }
  239.     return newstringobject(res);
  240. }
  241.  
  242. static object *
  243. mac_listdir(self, args)
  244.     object *self;
  245.     object *args;
  246. {
  247.     char *name;
  248.     object *d, *v;
  249.     DIR *dirp;
  250.     struct dirent *ep;
  251.     if (!getargs(args, "s", &name))
  252.         return NULL;
  253.     BGN_SAVE
  254.     if ((dirp = opendir(name)) == NULL) {
  255.         RET_SAVE
  256.         return mac_error();
  257.     }
  258.     if ((d = newlistobject(0)) == NULL) {
  259.         closedir(dirp);
  260.         RET_SAVE
  261.         return NULL;
  262.     }
  263.     while ((ep = readdir(dirp)) != NULL) {
  264.         v = newstringobject(ep->d_name);
  265.         if (v == NULL) {
  266.             DECREF(d);
  267.             d = NULL;
  268.             break;
  269.         }
  270.         if (addlistitem(d, v) != 0) {
  271.             DECREF(v);
  272.             DECREF(d);
  273.             d = NULL;
  274.             break;
  275.         }
  276.         DECREF(v);
  277.     }
  278.     closedir(dirp);
  279.     END_SAVE
  280.  
  281.     return d;
  282. }
  283.  
  284. static object *
  285. mac_lseek(self, args)
  286.     object *self;
  287.     object *args;
  288. {
  289.     int fd;
  290.     int where;
  291.     int how;
  292.     long res;
  293.     if (!getargs(args, "(iii)", &fd, &where, &how))
  294.         return NULL;
  295.     BGN_SAVE
  296.     res = lseek(fd, (long)where, how);
  297.     END_SAVE
  298.     if (res < 0)
  299.         return mac_error();
  300.     return newintobject(res);
  301. }
  302.  
  303. static object *
  304. mac_mkdir(self, args)
  305.     object *self;
  306.     object *args;
  307. {
  308.     return mac_strint(args, mkdir);
  309. }
  310.  
  311. static object *
  312. mac_open(self, args)
  313.     object *self;
  314.     object *args;
  315. {
  316.     char *path;
  317.     int mode;
  318.     int fd;
  319.     if (!getargs(args, "(si)", &path, &mode))
  320.         return NULL;
  321.     BGN_SAVE
  322.     fd = open(path, mode);
  323.     END_SAVE
  324.     if (fd < 0)
  325.         return mac_error();
  326.     return newintobject((long)fd);
  327. }
  328.  
  329. static object *
  330. mac_read(self, args)
  331.     object *self;
  332.     object *args;
  333. {
  334.     int fd, size;
  335.     object *buffer;
  336.     if (!getargs(args, "(ii)", &fd, &size))
  337.         return NULL;
  338.     buffer = newsizedstringobject((char *)NULL, size);
  339.     if (buffer == NULL)
  340.         return NULL;
  341.     BGN_SAVE
  342.     size = read(fd, getstringvalue(buffer), size);
  343.     END_SAVE
  344.     if (size < 0) {
  345.         DECREF(buffer);
  346.         return mac_error();
  347.     }
  348.     resizestring(&buffer, size);
  349.     return buffer;
  350. }
  351.  
  352. static object *
  353. mac_rename(self, args)
  354.     object *self;
  355.     object *args;
  356. {
  357.     return mac_2str(args, rename);
  358. }
  359.  
  360. static object *
  361. mac_rmdir(self, args)
  362.     object *self;
  363.     object *args;
  364. {
  365.     return mac_1str(args, rmdir);
  366. }
  367.  
  368. static object *
  369. mac_stat(self, args)
  370.     object *self;
  371.     object *args;
  372. {
  373.     struct macstat st;
  374.     char *path;
  375.     int res;
  376.     if (!getargs(args, "s", &path))
  377.         return NULL;
  378.     BGN_SAVE
  379.     res = macstat(path, &st);
  380.     END_SAVE
  381.     if (res != 0)
  382.         return mac_error();
  383.     return mkvalue("(llllllllll)",
  384.             (long)st.st_mode,
  385.             (long)st.st_ino,
  386.             (long)st.st_dev,
  387.             (long)st.st_nlink,
  388.             (long)st.st_uid,
  389.             (long)st.st_gid,
  390.             (long)st.st_size,
  391.             (long)st.st_atime,
  392.             (long)st.st_mtime,
  393.             (long)st.st_ctime);
  394. }
  395.  
  396. static object *
  397. mac_sync(self, args)
  398.     object *self;
  399.     object *args;
  400. {
  401.     int res;
  402.     if (!getnoarg(args))
  403.         return NULL;
  404.     BGN_SAVE
  405.     res = sync();
  406.     END_SAVE
  407.     if (res != 0)
  408.         return mac_error();
  409.     INCREF(None);
  410.     return None;
  411. }
  412.  
  413. static object *
  414. mac_unlink(self, args)
  415.     object *self;
  416.     object *args;
  417. {
  418.     return mac_1str(args, (int (*)(const char *))unlink);
  419. }
  420.  
  421. static object *
  422. mac_write(self, args)
  423.     object *self;
  424.     object *args;
  425. {
  426.     int fd, size;
  427.     char *buffer;
  428.     if (!getargs(args, "(is#)", &fd, &buffer, &size))
  429.         return NULL;
  430.     BGN_SAVE
  431.     size = write(fd, buffer, size);
  432.     END_SAVE
  433.     if (size < 0)
  434.         return mac_error();
  435.     return newintobject((long)size);
  436. }
  437.  
  438. static struct methodlist mac_methods[] = {
  439.     {"chdir",    mac_chdir},
  440.     {"close",    mac_close},
  441. #ifdef MPW
  442.     {"dup",        mac_dup},
  443. #endif
  444.     {"fdopen",    mac_fdopen},
  445.     {"getbootvol",    mac_getbootvol}, /* non-standard */
  446.     {"getcwd",    mac_getcwd},
  447.     {"listdir",    mac_listdir},
  448.     {"lseek",    mac_lseek},
  449.     {"mkdir",    mac_mkdir},
  450.     {"open",    mac_open},
  451.     {"read",    mac_read},
  452.     {"rename",    mac_rename},
  453.     {"rmdir",    mac_rmdir},
  454.     {"stat",    mac_stat},
  455.     {"sync",    mac_sync},
  456.     {"unlink",    mac_unlink},
  457.     {"write",    mac_write},
  458.  
  459.     {NULL,        NULL}         /* Sentinel */
  460. };
  461.  
  462.  
  463. void
  464. initmac()
  465. {
  466.     object *m, *d, *v;
  467.     
  468.     m = initmodule("mac", mac_methods);
  469.     d = getmoduledict(m);
  470.     
  471.     /* Initialize mac.error exception */
  472.     MacError = newstringobject("mac.error");
  473.     if (MacError == NULL || dictinsert(d, "error", MacError) != 0)
  474.         fatal("can't define mac.error");
  475. }
  476.